<template>
  <!-- 员工实名认证审核 -->
  <div>
    <!-- 表单控件和按钮 -->
    <div class="header">
      <el-form :inline="true" :model="params" class="demo-form-inline">
        <el-form-item>
          <el-button type="primary" @click="toAdd">申请</el-button>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="params.timer"
            value-format="timestamp"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
          />
          <el-select v-model="params.status" placeholder="请选择状态" clearable>
            <el-option label="通过" value="通过" />
            <el-option label="不通过" value="不通过" />
            <el-option label="未审核" value="未审核" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表单控件和按钮结束 -->

    <!-- 表格 -->
    <div class="border">
      <el-table v-loading="loading" :data="tableData" style="width: 100%">
        <el-table-column label="序号" type="index" :index="1" align="center" />
        <el-table-column prop="id" label="id" align="center" />
        <el-table-column prop="realname" label="姓名" align="center" />
        <el-table-column
          prop="applyTime"
          label="申请时间"
          width="180"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.applyTime | parseTime }}
          </template>
        </el-table-column>
        <el-table-column
          prop="idCard"
          label="身份证号"
          width="160"
          align="center"
        />
        <el-table-column
          prop="idcardPhotoPositive"
          label="身份证正面"
          align="center"
        >
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="right">
              <img
                slot="reference"
                :src="scope.row.idcardPhotoPositive"
                alt="加载失败"
                width="30px"
              />
              <img :src="scope.row.idcardPhotoPositive" alt="" width="200px" />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column
          prop="idcardPhotoNegative"
          label="身份证反面"
          align="center"
        >
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="right">
              <img
                slot="reference"
                :src="scope.row.idcardPhotoNegative"
                alt="加载失败"
                width="30px"
              />
              <img :src="scope.row.idcardPhotoNegative" alt="" width="200px" />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column
          prop="bankCard"
          label="银行卡号"
          align="center"
          width="180"
        />
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === '通过'" type="success"
              >通过</el-tag
            >
            <el-tag v-else-if="scope.row.status === '不通过'" type="danger"
              >不通过</el-tag
            >
            <el-tag v-else type="warning">未审核</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="success"
              icon="el-icon-edit"
              circle
              @click="toThrough(scope.row.id)"
            />
            <el-button
              type="danger"
              icon="el-icon-edit"
              circle
              @click="toNotThrough(scope.row.id)"
            />
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="todelete(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 表格结束 -->

    <!-- 分页 -->
    <div class="footer">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 分页结束 -->

    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="visible" @close="toClose">
      <el-form ref="myForm" :model="form" :rules="rules">
        <el-form-item
          prop="realname"
          label="申请人姓名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.realname" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="idCard"
          label="身份证号"
          :label-width="formLabelWidth"
        >
          <el-input v-model.number="form.idCard" autocomplete="off" />
        </el-form-item>
        <!-- 上传照片 -->
        <el-form-item
          label="身份证正面照片"
          :label-width="formLabelWidth"
          prop="idcardPhotoPositive"
          style="display: inline-block"
        >
          <el-upload
            class="avatar-uploader"
            :action="uploadFileURL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="imageUrl"
              :src="form.idcardPhotoPositive"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>

        <el-form-item
          label="身份证反面照片"
          :label-width="formLabelWidth"
          prop="idcardPhotoNegative"
          style="display: inline-block; margin-left: 100px"
        >
          <el-upload
            class="avatar-uploader"
            :action="uploadFileURL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="imageUrl"
              :src="form.idcardPhotoNegative"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>

        <el-form-item
          prop="bankCard"
          label="银行卡号"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.bankCard" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="toClose()">取 消</el-button>
        <el-button type="primary" @click="toSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>

<script>
import {
  queryCertification,
  submitData,
  cancelApplication,
  through,
  notThrough,
} from "@/api/certification";
import { uploadFileURL } from "@/utils/config";
export default {
  data() {
    return {
      tableData: [],
      // 表单对象和分页数据
      params: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      // 控制白屏加载动画
      loading: false,
      // 控制模态框显示与隐藏的变量
      visible: false,
      // 表单对象
      form: {},
      formLabelWidth: "120px",
      // 图片上传路径
      uploadFileURL,
      // 上传图片的变量
      imageUrl: "",
      // 模态框标题
      title: "",
      // 表单校验
      rules: {
        realname: [
          { required: true, message: "请输入申请人姓名", trigger: "blur" },
        ],
        idCard: [
          {
            required: true,
            message: "请输入身份证号",
            trigger: "blur",
            type: "number",
          },
          // { type: "number", message: "必须为数字" },
        ],
        idcardPhotoPositive: [
          {
            required: true,
            message: "请上传身份证反面照片",
            trigger: "change",
          },
        ],
        idcardPhotoNegative: [
          {
            required: true,
            message: "请上传身份证反面照片",
            trigger: "change",
          },
        ],
        bankCard: [
          { required: true, message: "请选择栏目", trigger: "change" },
        ],
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  computed: {},
  created() {
    this.getCertification();
  },
  mounted() {},
  methods: {
    // 添加按钮弹出模态框
    toAdd() {
      (this.visible = true),
        (this.title = "新增申请信息"),
        (this.form = {}),
        (this.imageUrl = "");
    },
    // 图片上传方法
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      if (!this.form.idcardPhotoPositive) {
        this.form.idcardPhotoPositive = res.data;
        this.form.idcardPhotoNegative = "";
      } else {
        this.form.idcardPhotoNegative = res.data;
      }

      // this.form.idcardPhotoNegative = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    // 模态框提交成功的方法
    async toSubmit() {
      // 校验表单
      this.$refs.myForm.validate(async (valid) => {
        if (valid) {
          // 发送保存或更新的请求
          const result = await submitData(this.form);
          // 提示框
          this.$notify({
            type: "success",
            message: result.message,
          });
          this.getCertification();
          this.toClose();
        } else {
          return false;
        }
      });
    },
    // 关闭模态框触发的回调
    toClose() {
      setTimeout(() => {
        // 重置表单
        this.$refs.myForm.resetFields();
        this.visible = false;
      }, 500);
      // clearTimeout();
    },

    // 查询按钮
    onSubmit() {
      this.params.page = 1;
      this.getCertification();
    },
    // 分页查询实名认证数据
    async getCertification() {
      const temp = { ...this.params, ...this.form };
      for (const key in temp) {
        if (!temp[key]) {
          delete temp[key];
        }
      }
      if (temp.timer) {
        temp.beginTime = this.params.timer[0];
        temp.endTime = this.params.timer[1];
        delete temp.timer;
        // {status page pagesize beginTime endtime}
      }
      // for (const key in this.params) {
      //   // key--属性名
      //   // this.params[key]--属性值
      //   if (!this.params[key]) {
      //     delete this.params[key];
      //   }
      // }
      const res = await queryCertification(temp);
      // console.log(res.data.list);
      this.tableData = res.data.list;
      this.total = res.data.total;
    },
    // 撤销申请
    async todelete(row) {
      const dele = await cancelApplication(row);
      // console.log(b);
      this.getCertification();
    },
    // 通过申请
    async toThrough(id) {
      console.log(id);
      const thr = await through({ id });
      this.getCertification();
    },
    // 不通过申请
    async toNotThrough(id) {
      console.log(id);
      const nothr = await notThrough({ id });
      this.getCertification();
    },

    // 页码条数发生改变的方法 默认参数pageSize
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.params.page = 1;
      this.getCertification();
    },

    // 当前页码发生改变触发的方法
    handleCurrentChange(page) {
      this.params.page = page;
      this.getCertification();
    },
  },
};
</script>
<style scoped>
</style>
